package com.itheima.web.manager;

import com.itheima.service.db.AnalysisByDayService;
import com.itheima.vo.AnalysisSummary1Vo;
import com.itheima.vo.AnalysisSummaryVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class AnalysisManager {

    @Reference
    private AnalysisByDayService analysisByDayService;

    //查询首页概要统计信息
    public AnalysisSummaryVo analysisSummary() {
        return analysisByDayService.analysisSummary();
    }

    public Map summaryByTime(String type, Long sd, Long ed) {
        //获取今年数据
        List<AnalysisSummary1Vo> thisYear = new ArrayList<>();
        Long thisYearSd = sd;
        Long thisYearEd = ed;
        Long oneDay = 1000 * 60 * 60 * 24L;
        while (thisYearSd < thisYearEd){
            String everyDay = new SimpleDateFormat("yyyy-MM-dd").format(new Date(thisYearSd));
            AnalysisSummary1Vo analysisSummary1Vo = new AnalysisSummary1Vo();
            analysisSummary1Vo.setTitle(everyDay);
            Integer amount = analysisByDayService.findByTime(everyDay, type);
            analysisSummary1Vo.setAmount(amount);
            thisYearSd += oneDay;
            thisYear.add(analysisSummary1Vo);
        }

        //去年开始时间
        Calendar c = Calendar.getInstance();
        c.setTime(new Date(sd));
        c.add(Calendar.YEAR, -1);
        Long lastYearSd = c.getTimeInMillis();

        //去年结束时间
        Calendar c1 = Calendar.getInstance();
        c1.setTime(new Date(ed));
        c1.add(Calendar.YEAR, -1);
        Long lastYearEd = c1.getTimeInMillis();

        //获取去年数据
        List<AnalysisSummary1Vo> lastYear = new ArrayList<>();
        while (lastYearSd < lastYearEd){
            String everyDay = new SimpleDateFormat("yyyy-MM-dd").format(new Date(lastYearSd));
            AnalysisSummary1Vo analysisSummary1Vo = new AnalysisSummary1Vo();
            analysisSummary1Vo.setTitle(everyDay);
            Integer amount = analysisByDayService.findByTime(everyDay, type);
            analysisSummary1Vo.setAmount(amount);
            lastYearSd += oneDay;
            lastYear.add(analysisSummary1Vo);
        }

        Map map = new HashMap();
        map.put("thisYear", thisYear);
        map.put("lastYear", lastYear);
        return map;

    }



}
